// /** 游戏数据 */
// /** 调试 */

import { _decorator } from 'cc';
import ShuZiHuaRongDao_Block from "./ShuZiHuaRongDao_Block";
import ShuZiHuaRongDao_GameFunction from "./ShuZiHuaRongDao_GameFunction";
import { ShuZiHuaRongDao_GameStorage } from "./ShuZiHuaRongDao_GameStorage";


export class ShuZiHuaRongDao_GameData {
    static W: number = 600;
    static H: number = 600;
    static row: number = 4; // 行数
    static col: number = 4; // 列数
    static size: number = ShuZiHuaRongDao_GameData.W / ShuZiHuaRongDao_GameData.row; // 方块的尺寸
    static arrBlocks = [];
    static Game_mode = `game_mode_3x3`;
    static step_number: number = 0;
    static game_start = false;
    static colors = [
        { id: 2, color: "#FFF3E7", Label_color: "#424952" },
        //        // { id: 4, color: "#FFEBCE", Label_color: "#424952" },
        { id: 8, color: "#FFC773", Label_color: "#424952" },
        //        // { id: 16, color: "#FFA663", Label_color: "#FFFFFF" },
        //        // { id: 32, color: "#FF8652", Label_color: "#FFFFFF" },
        //        // { id: 64, color: "#FF5D52", Label_color: "#FFFFFF" },
        { id: 128, color: "#C6CB7B", Label_color: "#FFFFFF" },
        { id: 256, color: "#94CF94", Label_color: "#313031" },
        { id: 512, color: "#5ACFAD", Label_color: "#313031" },
        //        // 修改后的部分（每个颜色使用下一级的颜色值）
        { id: 1024, color: "#52B6CF", Label_color: "#313031" },  // ← 原2048的浅蓝
        { id: 2048, color: "#528BCF", Label_color: "#FFFFFF" },   // ← 原4096的中蓝
        { id: 4096, color: "#7B7BCB", Label_color: "#FFFFFF" },   // ← 原8192的紫蓝
        { id: 8192, color: "#9D5ACF", Label_color: "#FFFFFF" },   // ← 原16384的紫
        { id: 16384, color: "#CF5AA7", Label_color: "#FFFFFF" }    // 新生成的紫红色
    ];
    static InitData() {
        ShuZiHuaRongDao_GameData.ClearData();

        ShuZiHuaRongDao_GameData.arrBlocks = [];

        if (ShuZiHuaRongDao_GameData.Game_mode === `game_mode_3x3`) {
            ShuZiHuaRongDao_GameData.row = 3;
            ShuZiHuaRongDao_GameData.col = 3;
            ShuZiHuaRongDao_GameData.size = ShuZiHuaRongDao_GameData.W / ShuZiHuaRongDao_GameData.row;
        }
        else if (ShuZiHuaRongDao_GameData.Game_mode === `game_mode_4x4`) {
            ShuZiHuaRongDao_GameData.row = 4;
            ShuZiHuaRongDao_GameData.col = 4;
            ShuZiHuaRongDao_GameData.size = ShuZiHuaRongDao_GameData.W / ShuZiHuaRongDao_GameData.row;
        }
        else if (ShuZiHuaRongDao_GameData.Game_mode === `game_mode_5x5`) {
            ShuZiHuaRongDao_GameData.row = 5;
            ShuZiHuaRongDao_GameData.col = 5;
            ShuZiHuaRongDao_GameData.size = ShuZiHuaRongDao_GameData.W / ShuZiHuaRongDao_GameData.row;
        }
        else if (ShuZiHuaRongDao_GameData.Game_mode === `game_mode_6x6`) {
            ShuZiHuaRongDao_GameData.row = 6;
            ShuZiHuaRongDao_GameData.col = 6;
            ShuZiHuaRongDao_GameData.size = ShuZiHuaRongDao_GameData.W / ShuZiHuaRongDao_GameData.row;
        }
        else if (ShuZiHuaRongDao_GameData.Game_mode === `game_mode_7x7`) {
            ShuZiHuaRongDao_GameData.row = 7;
            ShuZiHuaRongDao_GameData.col = 7;
            ShuZiHuaRongDao_GameData.size = ShuZiHuaRongDao_GameData.W / ShuZiHuaRongDao_GameData.row;
        }
        else if (ShuZiHuaRongDao_GameData.Game_mode === `game_mode_8x8`) {
            ShuZiHuaRongDao_GameData.row = 8;
            ShuZiHuaRongDao_GameData.col = 8;
            ShuZiHuaRongDao_GameData.size = ShuZiHuaRongDao_GameData.W / ShuZiHuaRongDao_GameData.row;
        }
        else if (ShuZiHuaRongDao_GameData.Game_mode === `game_mode_9x9`) {
            ShuZiHuaRongDao_GameData.row = 9;
            ShuZiHuaRongDao_GameData.col = 9;
            ShuZiHuaRongDao_GameData.size = ShuZiHuaRongDao_GameData.W / ShuZiHuaRongDao_GameData.row;
        }
        else if (ShuZiHuaRongDao_GameData.Game_mode === `game_mode_10x10`) {
            ShuZiHuaRongDao_GameData.row = 10;
            ShuZiHuaRongDao_GameData.col = 10;
            ShuZiHuaRongDao_GameData.size = ShuZiHuaRongDao_GameData.W / ShuZiHuaRongDao_GameData.row;
        }
    }
    static ClearData() {
        ShuZiHuaRongDao_GameData.step_number = 0;
        ShuZiHuaRongDao_GameData.game_start = false;

        ShuZiHuaRongDao_GameStorage.data[ShuZiHuaRongDao_GameData.Game_mode].current_data = {
            map: [],
            time: 0,
            step_number: 0,
        }
        ShuZiHuaRongDao_GameStorage.saveData();

        // ShuZiHuaRongDao_GameData.game_start = false;
        // ShuZiHuaRongDao_GameData.game_start = false;
        // ShuZiHuaRongDao_GameData.game_start = false;

        // ShuZiHuaRongDao_GameStorage.data[ShuZiHuaRongDao_GameData.Game_mode].score = 0;
        // ShuZiHuaRongDao_GameStorage.data[ShuZiHuaRongDao_GameData.Game_mode].map = [];
        // ShuZiHuaRongDao_GameStorage.saveData();
    }
    static Get_time_format(elapsedTime) {
        if (elapsedTime < 0) {
            elapsedTime = 0;
        }

        const totalSeconds = Math.floor(elapsedTime);
        const minutes = Math.floor(totalSeconds / 60);
        const seconds = totalSeconds % 60;
        return `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
    }
    //    /** 保存数据 */
    static SaveData() {
        let serialized = [];

        for (let index = 0; index < ShuZiHuaRongDao_GameData.arrBlocks.length; index++) {
            const block: ShuZiHuaRongDao_Block = ShuZiHuaRongDao_GameData.arrBlocks[index];
            let item = block.toJSON();
            serialized.push(item);
        }

        console.log(serialized);
        ShuZiHuaRongDao_GameStorage.data[ShuZiHuaRongDao_GameData.Game_mode].current_data.map = serialized;
        ShuZiHuaRongDao_GameStorage.saveData();
    }
    static async ReadData() {
        for (let index = 0; index < ShuZiHuaRongDao_GameStorage.data[ShuZiHuaRongDao_GameData.Game_mode].current_data.map.length; index++) {
            const item = ShuZiHuaRongDao_GameStorage.data[ShuZiHuaRongDao_GameData.Game_mode].current_data.map[index];
            let col = item.col;
            let row = item.row;
            let id = item.id;
            // await S2048_CardFunctions.指定地生成一个方块(col, row, id);


            // for (let r = 1; r <= row; r++) {
            //     for (let c = 1; c <= col; c++) {
            //         let num = id;
            //         if (r === row && c === col) {
            //             num = 0; // 右下空格
            //         } else {
            //             id++;
            //         }

            const block = await ShuZiHuaRongDao_GameFunction.CreateBlock(col, row, id);
            ShuZiHuaRongDao_GameData.arrBlocks.push(block);
            // }
            // }

        }
    }
    // static GetScore() {
    //     return ShuZiHuaRongDao_GameData.Score;
    // }
    // static SetScore(_score) {
    //     ShuZiHuaRongDao_GameData.Score = _score;
    //     ShuZiHuaRongDao_GameStorage.data[ShuZiHuaRongDao_GameData.Game_mode].score = _score;
    //     ShuZiHuaRongDao_GameStorage.saveData();
    // }
    // static Add_score(_score) {
    //     let new_score = ShuZiHuaRongDao_GameData.GetScore() + _score;
    //     ShuZiHuaRongDao_GameData.Update_score(new_score);
    // }
    // static Update_score(_score) {
    //     ShuZiHuaRongDao_GameData.SetScore(_score);
    //     if (ShuZiHuaRongDao_GameStorage.data[ShuZiHuaRongDao_GameData.Game_mode].high_score <= ShuZiHuaRongDao_GameData.Score) {
    //         ShuZiHuaRongDao_GameStorage.data[ShuZiHuaRongDao_GameData.Game_mode].high_score = ShuZiHuaRongDao_GameData.Score;
    //         ShuZiHuaRongDao_GameStorage.saveData();
    //     }
    // }
}

window["ShuZiHuaRongDao_GameData"] = ShuZiHuaRongDao_GameData;